//
//  RTStatusToolBar.swift
//  Weibo
//
//  Created by Brisinga on 16/3/9.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit

class RTStatusToolBar: UIView {

    private var retweetButton: UIButton!
    private var commentButton: UIButton!
    private var unlikeButton: UIButton!
    
    
    // 设置模型
    var statusViewModel: RTStatusViewModel? {
    
        didSet {
        
            retweetButton.setTitle(statusViewModel?.repostsCountStr, forState: UIControlState.Normal)
            commentButton.setTitle(statusViewModel?.commentsCountStr, forState: UIControlState.Normal)
            unlikeButton.setTitle(statusViewModel?.attitudesCountStr, forState: UIControlState.Normal)
        }
    }

    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        // 加载内部组件
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    // 加载内部组件
    private func setupUI() {
    
        retweetButton = addChildButton("timeline_icon_retweet", title: "转发")
        commentButton = addChildButton("timeline_icon_comment", title: "评论")
        unlikeButton = addChildButton("timeline_icon_unlike", title: "赞")
        
        // 添加分割线
        let split1 = addSplitView()
        let split2 = addSplitView()
        
        
        
        
        // 添加约束
        retweetButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        
        commentButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(retweetButton)
            make.leading.equalTo(retweetButton.snp_trailing)
            make.width.equalTo(unlikeButton)
        }
        
        unlikeButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(commentButton.snp_trailing)
            make.trailing.equalTo(self)
            make.top.bottom.equalTo(retweetButton)
        }
        
        split1.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetButton.snp_trailing)
        }
        
        split2.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentButton.snp_trailing)
        }
    }
    
    
    
    // 生成按钮
    private func addChildButton(imageName: String,title: String)->UIButton{
    
        let btn = UIButton(title: title, textColor: UIColor.grayColor(), textFont: 14)
        
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        // 设置背景颜色
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        
        addSubview(btn)
        
        return btn
    
    }
    
    
    // 懒加载分割线
    private func addSplitView()->UIView {
    
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        
        addSubview(imageView)
        
        return imageView
    }
}
